package com.extremelylargepackage.entity;

import com.extremelylargepackage.GameResources;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Style;
import android.util.Log;

public class Conclusion extends Entity
{
	private Paint _textPaint;
	private Paint _floatPaint;
	private boolean _victory;
	private boolean _concluded;
	private long _timeElapsed;
	private final static long CONCLUSION_DELAY = 2000;
	private final static long CONCLUSION_SHOWN = 4000;
	private final static long FADE_DURATION = 2000;
	private final static long FADE_START = (CONCLUSION_SHOWN + CONCLUSION_DELAY) - FADE_DURATION;
	private final static String VICTORY_TEXT = "Victory";
	private final static String DEFEAT_TEXT = "Defeat";
	
	public Conclusion()
	{
		_textPaint = new Paint();
		_textPaint.setColor(Color.WHITE);
		_textPaint.setTypeface(Typeface.DEFAULT_BOLD);
		_textPaint.setStyle(Style.STROKE);
		_textPaint.setTextSize(GameResources.getStatusFontSize());
		_textPaint.setShadowLayer(5, 0, 0, Color.BLACK);
		_textPaint.setAntiAlias(true);
		_floatPaint = new Paint();
		_floatPaint.setColor(Color.WHITE);
		_floatPaint.setStyle(Style.FILL);
	}
	
	public void setConclusion(boolean victory)
	{
		_victory = victory;
		_concluded = true;
	}
	
	public boolean getVictory()
	{
		return _victory;
	}
	
	public boolean canExit()
	{
		return _concluded && _timeElapsed >= CONCLUSION_DELAY + CONCLUSION_SHOWN;
	}
	
	@Override
	public void updateState(long time) 
	{
		if(_concluded)
			_timeElapsed += time;
	}

	@Override
	public void onCollision(Entity z) { /* nada */}

	@Override
	public void draw(Canvas c) 
	{
		if(_concluded && _timeElapsed >= CONCLUSION_DELAY)
		{
			String text;
			if(_victory)
				text = VICTORY_TEXT;
			else
				text = DEFEAT_TEXT;
			float x = (GameResources.SCREEN_WIDTH - _textPaint.measureText(text)) / 2;
			float y = GameResources.SCREEN_HEIGHT * .3f;
			c.drawText(text, x, y, _textPaint);
			if(_timeElapsed >= FADE_START)
			{
				int alpha = (int)(((_timeElapsed - FADE_START) / (float)FADE_DURATION) * 255f);
				_floatPaint.setAlpha(Math.min(alpha, 255));
				c.drawRect(0, 0, GameResources.SCREEN_WIDTH, GameResources.SCREEN_HEIGHT, _floatPaint);
			}
		}
	}

}
